package com.xiaohong.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.movie.dao.MovieMapper;
import com.xiaohong.movie.pojo.Movie;
import com.xiaohong.movie.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieMapper movieMapper;


    /**
     * 条件+分页查询Movie
     * @param movie 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Movie> findPage(Movie movie, int page, int size){
        // 构建查询条件
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<Movie>(movie);
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return movieMapper.selectPage(pg, queryWrapper);
    }

    /**
     * 分页查询Movie
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Movie> findPage(int page, int size){
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return movieMapper.selectPage(pg, null);
    }

    /**
     * 多条件搜索方法Movie
     * @param movie
     * @return
     */
    @Override
    public List<Movie> findList(Movie movie){
        // 构建查询条件
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<Movie>(movie);
        // 查询数据
        return movieMapper.selectList(queryWrapper);
    }

    /**
     * 根据ID删除Movie
     * @param id
     */
    @Override
    public void delete(Integer id){
        movieMapper.deleteById(id);
    }

    /**
     * 根据ID修改Movie
     * @param movie
     */
    @Override
    public void update(Movie movie){
        movieMapper.updateById(movie);
    }

    /**
     * 增加Movie
     * @param movie
     */
    @Override
    public void add(Movie movie){
        movieMapper.insert(movie);
    }

    /**
     * 根据ID查询Movie
     * @param id
     * @return
     */
    @Override
    public Movie findById(Integer id){
        return  movieMapper.selectById(id);
    }

    /**
     * 查询全部Movie数据
     * @return
     */
    @Override
    public List<Movie> findAll() {
        return movieMapper.selectList(null);
    }
}
